package com.novellotus.iov.carvice.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.novellotus.iov.carvice.R;


/**
 * Created by Rich on 2017/3/30.
 */
public class DotView extends View {
    private Paint p;
    private int width;
    private int height;
    private int dash;
    private int direction;//0是垂直虚线,1是横向虚线

    public DotView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DotView, 0, 0);
        try {
            direction = ta.getInteger(R.styleable.DotView_direction_line, 0);
        } finally {
            ta.recycle();
        }
        init(context);
    }

    public DotView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);
        p.setColor(context.getResources().getColor(R.color.green_line));

        final float scale = context.getResources().getDisplayMetrics().density;
        dash = (int) (3 * scale + 0.5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (direction == 0) {
            for (int i = 0; i < height; i += dash) {
                canvas.drawLine(0, i, 0, i += dash, p);
            }
        } else {
            for (int i = 0; i < width; i += dash) {
                canvas.drawLine(i, 0, i += dash, 0, p);
            }
        }

        super.onDraw(canvas);
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        p.setStrokeWidth(width);
        this.postInvalidate();
    }

}
